package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-11-25 17:08
 **/
public class LeetCode856 {
    //优化思路
    public int scoreOfParentheses(String s) {
        int res = 0;
        int depth = 0;
        int len = s.length();
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == '(') {
                depth++;
            } else {
                depth--;
                if (s.charAt(i - 1) == '(') {
                    res += 1 << depth;
                }
            }
        }
        return res;
    }

    //初始思路
    /*public int scoreOfParentheses(String s) {
        //score[i]存储第i层的值,从第一层开始
        int[] score = new int[26];
        char[] chars = s.toCharArray();
        //当前层数，没经过'('层数+1,否则层数-1
        int level = 0;
        //存储最大层数
        int maxLevel = 0;
        int len = s.length();
        for (int i = 0; i < len; i++) {
            char temp = chars[i];
            if (temp == '(') {
                level++;
                maxLevel = Math.max(level, maxLevel);
            } else {
                if (chars[i - 1] != ')') {
                    score[level] += 1;
                }
                level--;
            }
        }
        int res = 0;
        while (maxLevel > 0) {
            res += score[maxLevel];
            if (maxLevel != 1) {
                res *= 2;
            }
            maxLevel--;
        }
        return res;
    }*/

}
